package com.tjcd.project.library_base.base.RxJava;


import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import netpresenter.annotations.NetUnit;
import netpresenter.iface.INetListener;
import netpresenter.iface.INetUnit;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/9
 * @Description
 * @SpittingGrooves
 */
@NetUnit
public class RxJavaNetUnit implements INetUnit {
    /**
     * 被观察者
     */
    protected Observable mObservable;
    /**
     * 观察者
     */
    protected BaseObserver mObserver;

    @Override
    public INetUnit setObservable(Object observable) {
        mObservable = ((Observable) observable).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
        return this;
    }

    @Override
    public INetUnit request(INetListener observer) {
        if (null == mObservable) {
            throw new IllegalArgumentException("Not Fund INetUnit");
        }
        mObserver = (BaseObserver) observer;
        mObservable.subscribe(mObserver);
        return this;
    }

    @Override
    public void cancelRequest() {
        if (null != mObserver) {
            mObserver.dispose();
        }
    }
}
